package com.tm.media.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.model.entity.AdRoleMenuEntity;
import com.tm.media.model.mapper.AdRoleMenuMapper;
import com.tm.media.utils.utils.Result;
import com.tm.media.login.service.IAdRoleService;
import com.tm.media.model.dtos.ResponseResult;
import com.tm.media.model.entity.AdRole;
import com.tm.media.model.mapper.AdRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-12
 */
@Service
public class AdRoleServiceImpl extends ServiceImpl<AdRoleMapper,AdRole> implements IAdRoleService {
    @Autowired
    private AdRoleMapper adRoleMapper;
    @Autowired
    private AdRoleMenuMapper adRoleMenuMapper;

    @Override
    public ResponseResult selectList(Long pageNum, Long pageSize) {
        IPage<AdRole> iPage = new Page<AdRole>();
        iPage.setCurrent(pageNum);
        iPage.setSize(pageSize);
        IPage<AdRole> adUserIPage = baseMapper.selectPage(iPage, null);
        return ResponseResult.okResult(adUserIPage);
    }

    @Override
    public Result addRole(AdRole adRole) {
        QueryWrapper<AdRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",adRole.getRoleName());
        Integer count = adRoleMapper.selectCount(queryWrapper);
        if(count<1){
            adRoleMapper.insert(adRole);
            AdRoleMenuEntity adRoleMenuEntity = new AdRoleMenuEntity();
            adRoleMenuEntity.setRoleId(adRole.getId());
            adRoleMenuEntity.setMenuId(adRole.getParentId());
            adRoleMenuEntity.setCreatedTime(LocalDateTime.now());
            adRoleMenuMapper.insert(adRoleMenuEntity);
            return Result.SUCCESS(200,"新增成功");
        }
        return Result.FAIL(500,"用户名重复了");
    }

    @Override
    public List<AdRole> selectRoleList() {
        List<AdRole> adRoles = adRoleMapper.selectList(null);
        return adRoles;
    }
}
